import allure
import jsonpath
import logging

from utils.send_request_utils import send_sql_request

@allure.step("核心步骤3：http断言")
def asserts_http(case,res):
    # check有值时,实际结果是否等于预期结果
    if case["check"]:
        result = jsonpath.jsonpath(res.json(),case["check"])[0]
        logging.info(f"第3个: http断言中，将判断预期结果({case['expected']}) == 实际结果({result})？")
        allure.attach(f"预期结果({case['expected']}) == 实际结果({result})？","http断言数据")
        assert result == case["expected"]
    # check为空，判断是否包含即可,text为文本格式
    else:
        logging.info(f"第3个: http断言中，将判断预期结果({case['expected']}) 是否in 实际结果({res.text})中？")
        allure.attach(f"预期结果({case['expected']}) in 实际结果({res.text})？", "http断言数据")
        assert case["expected"] in res.text

def asserts_jdbc(case,res):
    # 当两个都有值时
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("核心步骤3：jdbc断言"):
            result = send_sql_request(case["sql_check"])
            logging.info(f"第3个: jdbc断言中，将判断预期结果({case['sql_expected']}) == 实际结果({result})？")
            allure.attach(f"预期结果({case['sql_expected']}) == 实际结果({result})？", "jdbc断言数据")
            assert result == case["sql_expected"]